package com.wlb365.admin.modules.block;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.admin.common.dto.R;
import com.wlb365.feign.api.dto.QueryBlockchainDTO;
import com.wlb365.feign.api.dto.QueryHistoryDTO;
import com.wlb365.feign.api.dto.QueryStateByIdDTO;
import com.wlb365.feign.api.dto.SysBusinessStateQueryDTO;
import com.wlb365.feign.api.dto.TemplateQueryByTypeDTO;
import com.wlb365.feign.api.result.BlockchainDataEntity;
import com.wlb365.feign.api.result.RBusinessBlockHistory;
import com.wlb365.feign.api.result.RBusinessStateData;
import com.wlb365.feign.api.result.RQueryStateByKeyData;
import com.wlb365.feign.api.result.RTemplateData;
import com.wlb365.feign.api.service.IBusinessBlockService;
import com.wlb365.feign.api.service.IBusinessStateService;
import com.wlb365.feign.api.service.ITemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 概述: 区块信息前端控制器
 * </p>
 *
 * @Author ysh
 */
@RestController
@RequestMapping("block/info")
public class BlockController {
    @Autowired
    private IBusinessBlockService businessBlockService;
    @Autowired
    private ITemplateService templateService;
    @Autowired
    private IBusinessStateService businessStateService;

    @RequiresPermissions("block:info:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("block/info");
    }

    @GetMapping(value = "list/data")
    public R listData(QueryBlockchainDTO dto) {
        Page<BlockchainDataEntity> page = businessBlockService.page(dto);
        return R.ok(page);
    }

    @GetMapping(value = "details")
    public ModelAndView details(String businessId) {
        QueryStateByIdDTO dto = new QueryStateByIdDTO();
        dto.setId(businessId);
        RQueryStateByKeyData rQueryStateByKeyData = businessBlockService.queryStateById(dto);

        ModelAndView mv = new ModelAndView("block/details");

        mv.addObject("businessId", businessId);
        mv.addObject("btype", rQueryStateByKeyData.getBtype());
        return mv;
    }

    /**
     * <p>
     * 概述:
     * </p>
     *
     * @throws
     * @Author ysh
     */
    @GetMapping(value = "schema")
    public R schema(String templateBtype) {
        TemplateQueryByTypeDTO dto = new TemplateQueryByTypeDTO();
        dto.setTemplateBtype(templateBtype);
        RTemplateData rTemplateData = templateService.queryStateByType(dto);
        if (StrUtil.isNotEmpty(rTemplateData.getTemplateData())) {

            JSONObject jsonObject = JSONUtil.parseObj(rTemplateData.getTemplateData());

            return R.ok(jsonObject);
        }
        return R.fail("查询模板失败");
    }

    /**
     * <p>
     * 概述: 查询当前区块数据
     * </p>
     *
     * @return com.wlb365.admin.common.dto.R
     * @Author ysh
     */
    @GetMapping(value = "data")
    public R data(String id) {

        QueryStateByIdDTO dto = new QueryStateByIdDTO();
        dto.setId(id);
        RQueryStateByKeyData rQueryStateByKeyData = businessBlockService.queryStateById(dto);

        if (StrUtil.isNotEmpty(rQueryStateByKeyData.getCurrentData())) {

            JSONObject jsonObject = JSONUtil.parseObj(rQueryStateByKeyData.getCurrentData());

            return R.ok(jsonObject);
        }
        return R.fail("当前区块数据失败");
    }

    @GetMapping("history")
    public ModelAndView history(String businessId) {
        ModelAndView mv = new ModelAndView("block/history");
        mv.addObject("businessId", businessId);
        return mv;
    }

    @GetMapping(value = "history/data")
    public R historyData(QueryHistoryDTO dto, String id) {
        dto.setBusinessId(id);
        Page<RBusinessBlockHistory> rBusinessBlockHistoryPage = businessBlockService.queryHistory(dto);
        return R.ok(rBusinessBlockHistoryPage);
    }


}
